// VedioPlayerSetUpDlg.cpp

#include "stdafx.h"
#include "VedioPlayer.h"
#include "VedioPlayerSetUpDlg.h"
#include "afxdialogex.h"
#include "VedioPlayerServerDlg.h"
#include "VedioPlayerLoginDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif//_DEBUG

BEGIN_MESSAGE_MAP(VedioPlayerSetUpDlg, CDialogEx)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BTN_SERVER, &VedioPlayerSetUpDlg::OnBnClickedBtnServer)
    ON_BN_CLICKED(IDC_BTN_CLIENT, &VedioPlayerSetUpDlg::OnBnClickedBtnClient)
    ON_BN_CLICKED(IDCANCEL, &VedioPlayerSetUpDlg::OnBnClickedCancel)
END_MESSAGE_MAP()

VedioPlayerSetUpDlg::VedioPlayerSetUpDlg(CWnd* pParent/* = NULL*/)
    : CDialogEx(VedioPlayerSetUpDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void VedioPlayerSetUpDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}


BOOL VedioPlayerSetUpDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    SetIcon(m_hIcon, TRUE);
    SetIcon(m_hIcon, FALSE);

    return TRUE;
}

void VedioPlayerSetUpDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this);

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

HCURSOR VedioPlayerSetUpDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

void VedioPlayerSetUpDlg::OnCloseServerDialog()
{
    GetDlgItem(IDC_BTN_SERVER)->EnableWindow(TRUE);
}

void VedioPlayerSetUpDlg::OnCloseClientDialog()
{
    GetDlgItem(IDC_BTN_CLIENT)->EnableWindow(TRUE);
}

void VedioPlayerSetUpDlg::OnBnClickedBtnServer()
{
    if (!VedioPlayerServerDlg::IsInitialize())
    {
        VedioPlayerServerDlg::GetInstance()->Create(IDD_DLG_SERVER, this);
        VedioPlayerServerDlg::GetInstance()->ShowWindow(SW_SHOW);
        GetDlgItem(IDC_BTN_SERVER)->EnableWindow(FALSE);
        VedioPlayerServerDlg::GetInstance()->GetDlgItem(IDC_BTN_SERVER_CLOSE_TCP)->EnableWindow(FALSE);

        CLOSE_SERVER_DIALOG_CALLBACK callback = std::bind(
            &VedioPlayerSetUpDlg::OnCloseServerDialog, this);
        VedioPlayerServerDlg::GetInstance()->SetCloseCallBack(callback);
    }
}


void VedioPlayerSetUpDlg::OnBnClickedBtnClient()
{
    if (!VedioPlayerLoginDlg::IsInitialize())
    {
        VedioPlayerLoginDlg::GetInstance()->Create(IDD_DLG_LOGIN, this);
        VedioPlayerLoginDlg::GetInstance()->ShowWindow(SW_SHOW);
        GetDlgItem(IDC_BTN_CLIENT)->EnableWindow(FALSE);

        CLOSE_CLIENT_DIALOG_CALLBACK callback = std::bind(
            &VedioPlayerSetUpDlg::OnCloseClientDialog, this);
        VedioPlayerLoginDlg::GetInstance()->SetCloseCallBack(callback);
    }
}

void VedioPlayerSetUpDlg::OnBnClickedCancel()
{
    if (VedioPlayerServerDlg::IsInitialize() || VedioPlayerLoginDlg::IsInitialize())
    {
        AfxMessageBox(L"Please close server/client dialog.");
    }
    else
    {
        CDialogEx::OnCancel();
    }
}
